package com.qianheng.kit.base.operation.orderoperation;

import com.qianheng.kit.base.operation.CheckResult;
import com.qianheng.kit.base.operation.OperationChecker;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;

import static com.qianheng.kit.base.operation.orderoperation.OrderOperationCheckerFactory.orderBizChecker;

@Getter
public enum OrderOperation {

    修改订单("editOrder", "修改订单",
            orderBizChecker("1", "12"),
            OrderOperationCheckerFactory.checkerTrue(true),
            new OperationChecker<OrderOperationCheckContext>() {
                @Override
                public CheckResult check(OrderOperationCheckContext checkContext) {
                    return new CheckResult(true,"成功");
                }
            }

    ),
    发货("consign", "发货",
            orderBizChecker("1", "12"),
            OrderOperationCheckerFactory.reverse("reverse真", OrderOperationCheckerFactory.checkerTrue(true)),
            checkContext -> {
                return new CheckResult(true,"成功");
            }
    ),
    测试算子("test", "测试算子",
            orderBizChecker("1", "12"),
            OrderOperationCheckerFactory.reverse("reverse", OrderOperationCheckerFactory.checkerTrue(false)),
            OrderOperationCheckerFactory.allMatch("全部匹配",
                    OrderOperationCheckerFactory.checkerTrue(true),
                    OrderOperationCheckerFactory.checkerTrue(false),
                    OrderOperationCheckerFactory.checkerTrue(true)
            )
    ),
    测试算子2("test2", "测试算子2",
            orderBizChecker("1", "12"),
            OrderOperationCheckerFactory.reverse("reverse", OrderOperationCheckerFactory.checkerTrue(false)),
            OrderOperationCheckerFactory.anyMatch("任意匹配",
                    OrderOperationCheckerFactory.checkerTrue(false),
                    OrderOperationCheckerFactory.checkerTrue(true),
                    OrderOperationCheckerFactory.checkerTrue(false)
            )
    ),
    ;

    /**
     * 值
     */
    private String value;
    /**
     * 描述
     */
    private String desc;
    /**
     * 校验器列表
     */
    private List<OperationChecker> checkerList;

    OrderOperation(String value, String desc, OperationChecker... checkers) {
        this.value = value;
        this.desc = desc;
        this.checkerList = Arrays.asList(checkers);
    }

    public CheckResult check(OrderOperationCheckContext checkContext) {
        CheckResult result = new CheckResult(true);
        for (OperationChecker operationChecker : checkerList) {
            CheckResult checkResult = operationChecker.check(checkContext);
            if (!checkResult.isSuccess()) {
                result.setSuccess(false);
                result.setReport(new StringBuilder().append(checkResult.getReport()));
                break;
            }
        }

        return result;
    }


}
